/*============================================================================*/
/*                   Tortoise Team					                          */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            MAL_ADC.h
* version:         1.0
* %created_by:      Andres Torres Garcia %
* %date_created:    Sunday May 20 13:41:01 2012 %
*=============================================================================*/
/* DESCRIPTION : Header file for uC Abstraction layer for ADC                 */
/*============================================================================*/
/* FUNCTION COMMENT : Describes the prototypes functions uC Abstraction layer */
/*   for ADC module               											  */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 25/05/2012 |                               | Andres Torres     */
/*	1.1		 | 29/05/2012 |								  | Andres Torres	  */
/*	1.2		 | 15/09/2012 |									Andres Torres     */
/* Integration under Continuus CM                                             */
/*==========================================================================*/

#ifndef MAL_ADC_H                               /* To avoid double inclusion */
#define MAL_ADC_H

/* Includes */
/* -------- */
/* Register definitions for selected microcontroller */
#include "stdtypedef.h"

/*======================================================*/ 
/* close variable declaration sections                  */
/*======================================================*/ 

/* Exported functions prototypes and macros */
/* ---------------------------------------- */

/* Exported defines */

/**
 * \defgroup Alignment ADC alignment
 * @{
 */
#define LEFT	0
#define RIGHT	1
/**@}*/

/**
 * \defgroup Modes ADC modes
 * @{
 */
#define ONE_SHOOT	0
#define SCAN		1
/**@}*/

/*! \def _32_MHz                                                                                  
  ADC Conversion time 1.25 us and analog clock of 32 MHz                                                                                     
 */
#define _32_MHz					0x00008606

#define CONVERSION_ADC_4_us_		0x00008663
#define CONVERSION_ADC_7_us_		0x000086E2

/**
 * \defgroup ADC precision channels.
 * @{
 */
#define ANP_0	0
#define ANP_1	1
#define ANP_2	2
#define ANP_3	3
#define ANP_4	4
#define ANP_5	5
#define ANP_6	6
#define ANP_7	7
#define ANP_8	8
#define ANP_9	9
#define ANP_10	10
#define ANP_11	11
#define ANP_12	12
#define ANP_13	13
#define ANP_14	14
#define ANP_15	15
/**@}*/

/**
 * \defgroup ADC standard channels.
 * @{
 */
#define ANS_0	32
#define ANS_1	33
#define ANS_2	34
#define ANS_3	35
#define ANS_4	36
#define ANS_5	37
#define ANS_6	38
#define ANS_7	39
#define ANS_8	40
#define ANS_9	41
#define ANS_10	42
#define ANS_11	43
#define ANS_12	44
#define ANS_13	45
#define ANS_14	46
#define ANS_15	47
/**@}*/

/**
 * \defgroup ADC_PCR precision channels.
 * @{
 */
#define ANP_PCR_0	20
#define ANP_PCR_1	21
#define ANP_PCR_2	22
#define ANP_PCR_3	23
#define ANP_PCR_4	48
#define ANP_PCR_5	49
#define ANP_PCR_6	50
#define ANP_PCR_7	51
#define ANP_PCR_8	52
#define ANP_PCR_9	53
#define ANP_PCR_10	54
#define ANP_PCR_11	55
#define ANP_PCR_12	56
#define ANP_PCR_13	57
#define ANP_PCR_14	58
#define ANP_PCR_15	59
/**@}*/

/* Adc channel types values for NCMR and CTR Channel Type parameter */

#define ADC_PRECISION_CHANNEL			0 	/* Precision channel */
#define ADC_EXTENDEDINT_CHANNEL			1 	/* Extended Internal channel */
#define ADC_EXTERNAL_CHANNEL			2 	/* External channel */

/* Ctu channel values for Adc channels */

#define CTU_CHANNEL_ANP0		0  	/*Adc Channel 0*/
#define CTU_CHANNEL_ANP1		1  	/*Adc Channel 1*/
#define CTU_CHANNEL_ANP2		2  	/*Adc Channel 2*/
#define CTU_CHANNEL_ANP3		3  	/*Adc Channel 3*/
#define CTU_CHANNEL_ANP4		4  	/*Adc Channel 4*/
#define CTU_CHANNEL_ANP5		5  	/*Adc Channel 5*/
#define CTU_CHANNEL_ANP6		6  	/*Adc Channel 6*/
#define CTU_CHANNEL_ANP7		7  	/*Adc Channel 7*/
#define CTU_CHANNEL_ANP8		8  	/*Adc Channel 8*/
#define CTU_CHANNEL_ANP9		9  	/*Adc Channel 9*/
#define CTU_CHANNEL_ANP10		10  /*Adc Channel 10*/
#define CTU_CHANNEL_ANP11		11  /*Adc Channel 11*/
#define CTU_CHANNEL_ANP12		12	/*Adc Channel 12*/
#define CTU_CHANNEL_ANP13		13 	/*Adc Channel 13*/
#define CTU_CHANNEL_ANP14		14 	/*Adc Channel 14*/
#define CTU_CHANNEL_ANP15		15 	/*Adc Channel 15*/

#define CTU_CHANNEL_ANS0		16 	/*Adc Channel 32*/
#define CTU_CHANNEL_ANS1		17 	/*Adc Channel 33*/
#define CTU_CHANNEL_ANS2		18 	/*Adc Channel 34*/
#define CTU_CHANNEL_ANS3		19 	/*Adc Channel 35*/
#define CTU_CHANNEL_ANS4		20 	/*Adc Channel 36*/
#define CTU_CHANNEL_ANS5		21 	/*Adc Channel 37*/
#define CTU_CHANNEL_ANS6		22 	/*Adc Channel 38*/
#define CTU_CHANNEL_ANS7		23 	/*Adc Channel 39*/


/**
 * \defgroup ADC_Watchdog useful macros
 * @{
 */
#define ENABLE_WDGH_INT		(ADC.WTIMR.B.MSKWDG0H=1)				// IE for high wdg
#define DISABLE_WDGH_INT	(ADC.WTIMR.B.MSKWDG0H=0)				// IE for high wdg
#define ENABLE_WDGL_INT		(ADC.WTIMR.B.MSKWDG0L=1)				// IE for low wdg
#define DISABLE_WDGL_INT	(ADC.WTIMR.B.MSKWDG0L=0)				// IE for low wdg
#define LOW_WDG_FLG			(ADC.WTISR.B.WDG0L)
#define CLEAR_LOW_WDG_FLG	(ADC.WTISR.B.WDG0L=1)
#define CLEAR_HIGH_WDG_FLG	(ADC.WTISR.B.WDG0H=1)
/**@}*/

/* Functions prototypes */

/*! Configure the pin as ADC channel. 
   \param ub_Port The pin.
   \sa Version 
  */
extern void vfnConfigPinAs_ADC_Channel(T_UBYTE ub_Port);

/*! Configure the type of ADC Mode.
   \param ub_mode The mode of the ADC. SCAN == 1 and ONE_SHOOT == 0.
   \sa Version 
  */
extern void vfnConfig_ADC_Mode(T_UBYTE ub_mode);

/*! Write the conversion data left/right-aligned.
   \param ub_justified Left 1 and right 0 alignment.
   \sa Version 
  */
extern void vfnJustifiedADCread(T_UBYTE ub_justified);

/*! Set the amount of channels to be sampling
   \param uw_channels0 Enable bits of normal sampling for channel 0 to 31 (precision channels)
   \param uw_channels1 Enable bits of normal sampling for channel 32 to 63 (extended internal channels)
   \param uw_channels2 Enable bits of normal sampling for channel 64 to 95 (external channels)
   \sa Version 
  */
extern void vfnSetAmountOfChannels(T_ULONG uw_channels0, T_ULONG uw_channels1, T_ULONG uw_channels2);

/*! Set the conversion time of the ADC channels
   \param ul_channels0 associated to internal precision channels (0 to 31)
   \param ul_channels1 associated to extended internal channels (from 32 to 63)
   \param ul_channels2 associated to external channels (from 64 to 95)
   \sa Version 
  */
extern void vfnSetConvertionTime(T_ULONG ul_channels0, T_ULONG ul_channels1, T_ULONG ul_channels2);

/*! Start the convertion of the channels enabled
   \sa Version 
  */
extern void vfnADC_StartConvertion(void);

/*!  Check if the ADC convertion is complete
   \param	ub_pin ADC Channel	
   \return True if the convertion is complete, otherwise false.
   \sa Version 
  */
extern T_UBYTE ADCConvertionComplete(T_UBYTE ub_pin);

/*! Read the value of the ADC Channel 
   \param ub_pin ADC Channel
   \return Value of the ADC Channel
   \sa Version 
  */
extern T_UWORD	ADCReadValue(T_UBYTE ub_pin);

/*! Init the ADC watchdog low threshold 
   \param u8WtdChannel ADC watchdog channel
   \param u32AdcChannel ADC channel
   \param u32LowTreshold Low threshold value
   \sa Version 
  */
extern void vfnInit_Adc_WatchdogL(T_UBYTE ub_WtdChannel, T_ULONG ul_AdcChannel, T_ULONG ul_LowTreshold);

/*! Read the value of the ADC Channel 
   \param u8WtdChannel ADC watchdog channel
   \param u32AdcChannel ADC channel
   \param u32HighTreshold High threshold value
   \sa Version 
  */
extern void vfnInit_Adc_WatchdogH(T_UBYTE ub_WtdChannel, T_ULONG ul_AdcChannel, T_ULONG ul_HighTreshold);

/*! Enable the interruption of the ADC watchdog channel 0
   \sa Version 
  */
extern void vfnEnable_Adc_Watchdog_Channel0(void);

/*! Disable the interruption of the ADC watchdog channel 0
   \sa Version 
  */
extern void vfnDisable_Adc_Watchdog_Channel0(void);

/*! Stop the ADC convertion.
   \sa Version 
  */
extern void vfnADC_StopConvertion(void);

/*! Enable the ADC cross trigger
   \sa Version 
  */
extern void vfnEnable_Adc_Cross_Trigger(void);

/*! Disable the ADC cross trigger
   \sa Version 
  */
extern void vfnDisable_Adc_Cross_Trigger(void);

/*! Configuere the eMIOS to trigger the ADC
   \sa Version 
  */
extern void vfnEnable_Trigger_Emios_Adc(T_UBYTE ubEmiCha,T_UBYTE ubCtuCha);

/*! Enable the interrupt of the ADC CTU
   \sa Version 
  */
extern void vfnEnable_Adc_Ctu_Interrupt(void);

/*! Disable the interrupt of the ADC CTU
   \sa Version 
  */
extern void vfnDisable_Adc_Ctu_Interrupt(void);

/*! Enable the ADC single shot interrupt
   \sa Version 
  */
extern void vfnEnable_Adc_SingleShot_Interrupt(void);

/*! Disable the ADC single shot interrupt
   \sa Version 
  */
extern void vfnDisable_Adc_SingleShot_Interrupt(void);

/*!
  \def ENABLE_INT_ADC_CHN0(typeADC)
  Enable the interruption of the ADC channel 0 of the type \a typeADC.
*/
#define ENABLE_INT_ADC_CHN0(typeADC)				(ADC.CIMR[(typeADC)].B.CIM0 = 1)

/*!
  \def DISABLE_INTE_CHN0(typeADC)
  Disable the interruption of the ADC channel 0 of the type \a typeADC.
*/
#define DISABLE_INTE_CHN0(typeADC)					(ADC.CIMR[(typeADC)].B.CIM0 = 0)

#endif